import axios from "axios";
import { getToken } from "@/utils/storage";
const request = axios.create({
  baseURL: "http://toutiao.itheima.net",
  timeout: 5000,
});
// 请求拦截
request.interceptors.request.use(
  (config) => {
    if (getToken()) {
      config.headers.Authorization = "Bearer " + getToken();
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
// 响应拦截
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (err) => {
    console.log(err);
    // 可选链运算符===err&&err.response$$err.response.data&&err.response.data.message
    // if (err?.response?.data?.message) {
    //   Toast.fail(err.response.data.message);
    // }
    return Promise.reject(err);
  }
);
export default request;
